package struts2.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import DAO.ConDB;
import DAO.Dao_addService;
import DAO.ServiceReferenceDAO;
import Entity.Funding;
import Entity.ReferenceData;
import Entity.Services;

import com.opensymphony.xwork2.ActionSupport;

public class AddServiceAction extends ActionSupport {
	private int id;
	private String sv_name;
	private String sub_type;
	private String contacts;
	private String sv_description;
	private String client_des;
	private String sv_attenace;
	private String sv_start_expect;
	private String sv_start;
	private String sv_end;
	private int sv_ed_year;
	private int sv_ed_month;
	private boolean active;
	private String sv_full_des;
	private String dept_code;
	private String sv_type;
	private String sv_des_delivery;
	private String sv_contract_code;
	private String sv_contract_value;
	private boolean contract_state;
	private String sv_state;

	private ArrayList<Services> list;

	public String getSv_state() {
		return sv_state;
	}

	public void setSv_state(String sv_state) {
		this.sv_state = sv_state;
	}

	public boolean isContract_state() {
		return contract_state;
	}

	public void setContract_state(boolean contract_state) {
		this.contract_state = contract_state;
	}

	private String method;
	private String actives;

	public String getSv_start_expect() {
		return sv_start_expect;
	}

	public void setSv_start_expect(String sv_start_expect) {
		this.sv_start_expect = sv_start_expect;
	}

	public String getSv_start() {
		return sv_start;
	}

	public void setSv_start(String sv_start) {
		this.sv_start = sv_start;
	}

	public String getSv_end() {
		return sv_end;
	}

	public void setSv_end(String sv_end) {
		this.sv_end = sv_end;
	}

	public String getContacts() {
		return contacts;
	}

	public void setContacts(String contacts) {
		this.contacts = contacts;
	}

	public String getActives() {
		return actives;
	}

	public void setActives(String actives) {
		this.actives = actives;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getSv_name() {
		return sv_name;
	}

	public void setSv_name(String sv_name) {
		this.sv_name = sv_name;
	}

	public String getSub_type() {
		return sub_type;
	}

	public void setSub_type(String sub_type) {
		this.sub_type = sub_type;
	}

	public String getSv_description() {
		return sv_description;
	}

	public void setSv_description(String sv_description) {
		this.sv_description = sv_description;
	}

	public String getClient_des() {
		return client_des;
	}

	public void setClient_des(String client_des) {
		this.client_des = client_des;
	}

	public String getSv_attenace() {
		return sv_attenace;
	}

	public void setSv_attenace(String sv_attenace) {
		this.sv_attenace = sv_attenace;
	}

	public int getSv_ed_year() {
		return sv_ed_year;
	}

	public void setSv_ed_year(int sv_ed_year) {
		this.sv_ed_year = sv_ed_year;
	}

	public int getSv_ed_month() {
		return sv_ed_month;
	}

	public void setSv_ed_month(int sv_ed_month) {
		this.sv_ed_month = sv_ed_month;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public String getSv_full_des() {
		return sv_full_des;
	}

	public void setSv_full_des(String sv_full_des) {
		this.sv_full_des = sv_full_des;
	}

	public String getDept_code() {
		return dept_code;
	}

	public void setDept_code(String dept_code) {
		this.dept_code = dept_code;
	}

	public String getSv_type() {
		return sv_type;
	}

	public void setSv_type(String sv_type) {
		this.sv_type = sv_type;
	}

	public String getSv_des_delivery() {
		return sv_des_delivery;
	}

	public void setSv_des_delivery(String sv_des_delivery) {
		this.sv_des_delivery = sv_des_delivery;
	}

	public String getSv_contract_code() {
		return sv_contract_code;
	}

	public void setSv_contract_code(String sv_contract_code) {
		this.sv_contract_code = sv_contract_code;
	}

	public String getSv_contract_value() {
		return sv_contract_value;
	}

	public void setSv_contract_value(String sv_contract_value) {
		this.sv_contract_value = sv_contract_value;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	private ArrayList<ReferenceData> listAttend;
	private ArrayList<ReferenceData> listServiceType;
	private ArrayList<ReferenceData> listMethod;

	public ArrayList<ReferenceData> getListAttend() {
		return listAttend;
	}

	public ArrayList<Services> getList() {
		return list;
	}

	public void setList(ArrayList<Services> list) {
		this.list = list;
	}

	public void setListAttend(ArrayList<ReferenceData> listAttend) {
		this.listAttend = listAttend;
	}

	public ArrayList<ReferenceData> getListServiceType() {
		return listServiceType;
	}

	public void setListServiceType(ArrayList<ReferenceData> listServiceType) {
		this.listServiceType = listServiceType;
	}

	public ArrayList<ReferenceData> getListMethod() {
		return listMethod;
	}

	public void setListMethod(ArrayList<ReferenceData> listMethod) {
		this.listMethod = listMethod;
	}

	// Tuan
	private String selectedid;

	public String getSelectedid() {
		return selectedid;
	}

	public void setSelectedid(String selectedid) {
		this.selectedid = selectedid;
	}

	//Tuyen
	
	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public Date getRequiredby() {
		return requiredby;
	}

	public void setRequiredby(Date requiredby) {
		this.requiredby = requiredby;
	}

	public Date getCompleteddate() {
		return completeddate;
	}

	public void setCompleteddate(Date completeddate) {
		this.completeddate = completeddate;
	}

	public Date getDonationdate() {
		return donationdate;
	}

	public void setDonationdate(Date donationdate) {
		this.donationdate = donationdate;
	}

	public java.sql.Date getSqlstart() {
		return sqlstart;
	}

	public void setSqlstart(java.sql.Date sqlstart) {
		this.sqlstart = sqlstart;
	}

	public java.sql.Date getSqlend() {
		return sqlend;
	}

	public void setSqlend(java.sql.Date sqlend) {
		this.sqlend = sqlend;
	}

	public java.sql.Date getSqlrequiredby() {
		return sqlrequiredby;
	}

	public void setSqlrequiredby(java.sql.Date sqlrequiredby) {
		this.sqlrequiredby = sqlrequiredby;
	}

	public java.sql.Date getSqlcompleteddate() {
		return sqlcompleteddate;
	}

	public void setSqlcompleteddate(java.sql.Date sqlcompleteddate) {
		this.sqlcompleteddate = sqlcompleteddate;
	}

	public java.sql.Date getSqldonationdate() {
		return sqldonationdate;
	}

	public void setSqldonationdate(java.sql.Date sqldonationdate) {
		this.sqldonationdate = sqldonationdate;
	}

	public int getFundingId() {
		return fundingId;
	}

	public void setFundingId(int fundingId) {
		this.fundingId = fundingId;
	}

	public int getServiceid() {
		return serviceid;
	}

	public void setServiceid(int serviceid) {
		this.serviceid = serviceid;
	}

	public String getFundingSource() {
		return fundingSource;
	}

	public void setFundingSource(String fundingSource) {
		this.fundingSource = fundingSource;
	}

	public String getContact2() {
		return contact2;
	}

	public void setContact2(String contact2) {
		this.contact2 = contact2;
	}

	public int getFundingAmount() {
		return fundingAmount;
	}

	public void setFundingAmount(int fundingAmount) {
		this.fundingAmount = fundingAmount;
	}

	public String getFundingStart() {
		return fundingStart;
	}

	public void setFundingStart(String fundingStart) {
		this.fundingStart = fundingStart;
	}

	public String getFundingEnd() {
		return fundingEnd;
	}

	public void setFundingEnd(String fundingEnd) {
		this.fundingEnd = fundingEnd;
	}

	public String getFundingNeeds() {
		return fundingNeeds;
	}

	public void setFundingNeeds(String fundingNeeds) {
		this.fundingNeeds = fundingNeeds;
	}

	public Boolean getFundingContinuationNeeded() {
		return fundingContinuationNeeded;
	}

	public void setFundingContinuationNeeded(Boolean fundingContinuationNeeded) {
		this.fundingContinuationNeeded = fundingContinuationNeeded;
	}

	public int getFundingContinuationAmount() {
		return fundingContinuationAmount;
	}

	public void setFundingContinuationAmount(int fundingContinuationAmount) {
		this.fundingContinuationAmount = fundingContinuationAmount;
	}

	public String getFundingContinuationDetails() {
		return fundingContinuationDetails;
	}

	public void setFundingContinuationDetails(String fundingContinuationDetails) {
		this.fundingContinuationDetails = fundingContinuationDetails;
	}

	public String getFundraisingForText() {
		return fundraisingForText;
	}

	public void setFundraisingForText(String fundraisingForText) {
		this.fundraisingForText = fundraisingForText;
	}

	public String getFundraisingWhy() {
		return fundraisingWhy;
	}

	public void setFundraisingWhy(String fundraisingWhy) {
		this.fundraisingWhy = fundraisingWhy;
	}

	public String getFundraisingNeeds() {
		return fundraisingNeeds;
	}

	public void setFundraisingNeeds(String fundraisingNeeds) {
		this.fundraisingNeeds = fundraisingNeeds;
	}

	public String getFundraisingRequiredBy() {
		return fundraisingRequiredBy;
	}

	public void setFundraisingRequiredBy(String fundraisingRequiredBy) {
		this.fundraisingRequiredBy = fundraisingRequiredBy;
	}

	public Boolean getFundraisingComplete() {
		return fundraisingComplete;
	}

	public void setFundraisingComplete(Boolean fundraisingComplete) {
		this.fundraisingComplete = fundraisingComplete;
	}

	public String getFundraisingCompletedDate() {
		return fundraisingCompletedDate;
	}

	public void setFundraisingCompletedDate(String fundraisingCompletedDate) {
		this.fundraisingCompletedDate = fundraisingCompletedDate;
	}

	public Boolean getFundraisingDonorAnonymous() {
		return fundraisingDonorAnonymous;
	}

	public void setFundraisingDonorAnonymous(Boolean fundraisingDonorAnonymous) {
		this.fundraisingDonorAnonymous = fundraisingDonorAnonymous;
	}

	public int getFundraisingDonorAmount() {
		return fundraisingDonorAmount;
	}

	public void setFundraisingDonorAmount(int fundraisingDonorAmount) {
		this.fundraisingDonorAmount = fundraisingDonorAmount;
	}

	public String getFundraisingDonationDate() {
		return fundraisingDonationDate;
	}

	public void setFundraisingDonationDate(String fundraisingDonationDate) {
		this.fundraisingDonationDate = fundraisingDonationDate;
	}

	public Boolean getFundraisingDonationIncremental() {
		return fundraisingDonationIncremental;
	}

	public void setFundraisingDonationIncremental(
			Boolean fundraisingDonationIncremental) {
		this.fundraisingDonationIncremental = fundraisingDonationIncremental;
	}
	Date start;
	Date end;
	Date requiredby;
	Date completeddate;
	Date donationdate;
	java.sql.Date sqlstart;
	java.sql.Date sqlend;
	java.sql.Date sqlrequiredby;
	java.sql.Date sqlcompleteddate;
	java.sql.Date sqldonationdate;
	private int fundingId=1;
	private int serviceid;
	private String fundingSource;
	//private String fundingContactDetails;
	private String contact2;
	private int fundingAmount;
	private String fundingStart="2013-07-10";
	private String fundingEnd="2013-07-10";
	private String fundingNeeds;
	private Boolean fundingContinuationNeeded;
	private int fundingContinuationAmount=10;
	private String fundingContinuationDetails="contact";
	private String fundraisingForText;
	private String fundraisingWhy;
	private String fundraisingNeeds;
	private String fundraisingRequiredBy="2013-07-10";
	private Boolean fundraisingComplete;
	private String fundraisingCompletedDate="2013-07-10";
	private Boolean fundraisingDonorAnonymous;
	private int fundraisingDonorAmount;
	private String fundraisingDonationDate="2013-07-10";
	private Boolean fundraisingDonationIncremental;
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		if (Dao_addService.checkName(sv_name) || sv_name.equals("")
				|| sv_name.equals(null)) {
			Dao_addService dao = new Dao_addService();
			listAttend = dao.getList("sv_att");
			listServiceType = dao.getList("sv_type");
			listMethod = dao.getList("sv_method");
			System.out.println("loi insert");
			return ERROR;
		} else {

			id = Dao_addService.getId();
			System.out.println("id :" + id);
			if (actives.equals("yes"))
				active = true;
			else
				active = false;
			contract_state = Boolean.parseBoolean(sv_state);

			int kq = Dao_addService
					.insert(id, sv_name, sub_type, contacts, sv_description,
							client_des, sv_attenace, sv_start_expect, sv_start,
							sv_end, sv_ed_year, sv_ed_month, active,
							sv_full_des, dept_code, sv_type, sv_des_delivery,
							sv_contract_code, sv_contract_value,
							contract_state, method);
			System.out.println("Insert thanh cong");
			if(kq>-1){
				System.out.println("Insert Tuan : "+id);
				System.out.println("selectid="+selectedid);
				String refID[]=selectedid.split(" ");
				if(refID!=null && refID.length>0){
					ServiceReferenceDAO srd=new ServiceReferenceDAO();
					srd.saveServiceReference(id, refID);
				}
			}
			
			//tuyen
			if(sub_type.equals("Funding")){
				if(!contact2.equals("")&& fundingAmount!=0){
					DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
					 start=df.parse(fundingStart);
					 sqlstart = new java.sql.Date(start.getTime());
					 end=df.parse(fundingEnd);
					 sqlend = new java.sql.Date(end.getTime());
					 requiredby=df.parse(fundraisingRequiredBy);
					 sqlrequiredby = new java.sql.Date(requiredby.getTime());
					completeddate=df.parse(fundraisingCompletedDate);
					sqlcompleteddate = new java.sql.Date(completeddate.getTime());
					donationdate =df.parse(fundraisingDonationDate);
					sqldonationdate = new java.sql.Date(donationdate.getTime());
					ConDB con=new ConDB();
					serviceid =id;
					con.deleteFunding(serviceid);
					con.saveFunding(new Funding(fundingId,
							serviceid,
							fundingSource,
							contact2,
							//fundingcontact2Details,
							fundingAmount,
							sqlstart,
							sqlend,
							fundingNeeds,
							fundingContinuationNeeded,
							fundingContinuationAmount,
							fundingContinuationDetails,
							fundraisingForText,
							fundraisingWhy,
							fundraisingNeeds,
							sqlrequiredby,
							fundraisingComplete,
							sqlcompleteddate,
							fundraisingDonorAnonymous,
							fundraisingDonorAmount,
							sqldonationdate,
							fundraisingDonationIncremental));
				
				}
			}
			
			list = Dao_addService.getList();
			return SUCCESS;
		}
		
	}

}
